package com.service.abolishing.controller;

import com.service.abolishing.dto.EnvFactorDto;
import com.service.abolishing.entity.EnvFactor;
import com.service.abolishing.service.EnvFactorService;
import com.service.config.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.service.config.constant.Constant.*;

/**
 * 设备环境因子表(EnvFactor)表控制层
 *
 * @author makejava
 * @since 2021-10-21 14:14:33
 */
@RestController
@RequestMapping("envFactor")
@Api(value = "环境因子接口", tags = "环境因子接口")
public class EnvFactorController {
    /**
     * 服务对象
     */
    @Resource
    private EnvFactorService envFactorService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 查询基站的环境因子
     * @param cloudAuthToken
     * @return
     */
    @GetMapping("getEnvFactor")
    @ApiOperation(value = "查询基站的环境因子")
    @ResponseBody
    public Result getEnvFactor(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                               @RequestParam String apId) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                EnvFactor envFactor = new EnvFactor();
                envFactor.setManageInfoId(userId);
                envFactor.setApId(apId);
                List<EnvFactor> envFactors = envFactorService.queryAll(envFactor);
                return Result.ok().setData(envFactors);
            }else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
    /**
     * 修改基站的环境因子
     * @param cloudAuthToken
     * @return
     */
    @PostMapping("freshEnvFactor")
    @ApiOperation(value = "修改基站的环境因子")
    @ResponseBody
    public Result freshEnvFactor(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                 @RequestBody EnvFactorDto envFactorDto) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                EnvFactor envFactor = ModelMapperUtil.strictMap(envFactorDto,EnvFactor.class);
                envFactor.setManageInfoId(userId);
                envFactorService.update(envFactor);
                return Result.ok();
            }else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
    /**
     * 修改基站的环境因子
     * @param cloudAuthToken
     * @return
     */
    @PostMapping("addEnvFactor")
    @ApiOperation(value = "新增基站的环境因子")
    @ResponseBody
    public Result addEnvFactor(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                 @RequestBody EnvFactorDto envFactorDto) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                EnvFactor envFactor = ModelMapperUtil.strictMap(envFactorDto,EnvFactor.class);
                envFactor.setManageInfoId(userId);
                envFactor.setId(IdUtil.getStringId());
                envFactorService.insert(envFactor);
                return Result.ok();
            }else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
    /**
     * 修改基站的环境因子
     * @param cloudAuthToken
     * @return
     */
    @PostMapping("delEnvFactor")
    @ApiOperation(value = "新增基站的环境因子")
    @ResponseBody
    public Result delEnvFactor(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                               @RequestParam String eId) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                envFactorService.deleteById(eId);
                return Result.ok();
            }else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
}
